<?php
 /*
  *运营商模型类
  */
 namespace Admin\Model; 
 use Think\Model;
 class DeviceModel extends Model{

  public function index($arg=array())
  { 
          //获取所有设备类型
    $devicetype=M('config')->where('id=%d',2)->getField('value');
    $devicetype=unserialize($devicetype);
    $page=intval($arg['page']);
    $page=!empty($page)?$page:1; 
       $yid='';
       $yid.=$_SESSION['yid'];
         ////获取该运营商下的搜有运营商
  $yids=M('yunying_user')->where("pid='%d'",$_SESSION['yid'])->getField("yid",true); 
      $yid.=', 0';
$yid.=implode(",",array_values($yids));
    $condition=' 1=1 ';
    if(!empty($yid))
    {
     $condition.='  AND a.yid in ('.$yid.')';
   
   
   }
   if(!empty($arg['keywords'])){

     $condition.=" AND b.companyname like '%".$arg['keywords']."%' or a.devicecode like '%".$arg['keywords']."%'";

   }


   if($arg['type'])
   {
   $arg['typeid']=intval($arg['typeid']);
     
     $condition.=' And a.tiypeid='.$arg['typeid'];
   }

   if($arg['isfen']!==99 && !empty($arg['isfen']))
   {
     switch ($arg['isfen'])
     {
                //未分配机器 ，shipid 为0
      case 2:
                   /// $where['a.shopid']=array('eq',0);
      $condition.=' AND a.shopid= 0';
      break;
                //已分配机器 ，shipid 不为0
      case 1:
                    ///$where['a.shopid']=array('neq',0);
      $condition.=' AND a.shopid <> 0';
      break;

    }

  }



  ////获取该运营商下的所有设备
  

  $count=M('')->query("select  count(*) as count from cart_device a left join cart_shop_user b  on a.shopid=b.shopid where".$condition);



  $pagearr= getPageArr($page,$pagesize,$count[0]['count']);
  //var_dump($pagearr);die;

  $start=$pagearr['start'];
  $pagesize=$pagearr['pagesize'];
  $start=$pagearr['start'];

  $sql='select a.devicecode,a.tiypeid,b.companyname,a.deviceid,a.shopid,a.yid from cart_device
  a left join cart_shop_user b  on a.shopid=b.shopid where'.$condition." limit  $start , $pagesize ";

  $result=M('')->query($sql);


  foreach ($result as $key => &$value) {
    $typeid=intval($value['tiypeid']);

    $value['devicetype']=$devicetype[$typeid];
    if(!empty($value['companyname'])|| $value['yid']!=$_SESSION['yid'])
    {
      $value['isfen']='已分配';


    }else
    {   $value['companyname']='';
    $value['isfen']='未分配';
  }
}

$temp['pagearr']=$pagearr;
$temp['devices']=!empty($result)?$result:'';
return $temp;







}
}